#### Extract summary statistics from the data

## Setup the environment

setwd("~/research/coffee-dataverse/src")

do.origspec <- T
source("load.R", encoding="iso-8859-1")

## Perform the calculations

df$obsyield <- df$total.produce / df$total.harvest

labels <- c("Production (MT)", "Harvested area (Ha)", "Obseved Yield (MT/Ha)", "Average Min.", "Average Max.", "GDDs / 1000", "EDDs / 1000", "Precip. (m)")
cols <- c('total.produce', 'total.harvest', 'obsyield', 'tmin', 'tmax', 'gdd1000', 'edd1000', 'prcp')

tdf <- data.frame()
for (kk in 1:length(cols)) {
    col <- cols[kk]
    N <- sum(!is.na(df[, col]))
    mu <- mean(df[, col], na.rm=T)
    sigma <- sd(df[, col], na.rm=T)
    lo <- min(df[, col], na.rm=T)
    hi <- max(df[, col], na.rm=T)
    tdf <- rbind(tdf, data.frame(Statistic=labels[kk], N, mean=mu, `Std. Dev.`=sigma, Min=lo, Max=hi))
}

## Print the table

library(xtable)

print(xtable(tdf), include.rownames=F)
